using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Toolkit.Collections.Helpers;
using CoachingOrganizer.Logic.Models.Tools;
using CoachingOrganizer.Logic.Models.Tools.MoodLevel;
using CoachingOrganizer.Logic.Models.Common;
using CoachingOrganizer.Logic.Models.Tools.EmotionsPack;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;

namespace CoachingOrganizer.Android.Adapters
{
    public class ToolsAdapter : BaseAdapter
    {
        private Activity _context;
        private List<ToolModelBase> _items = new List<ToolModelBase>();
        private GoalModel _model;

        public ToolsAdapter(Activity context, GoalModel model)
        {
            _context = context;
            _model = model;
            model.ToolsChanged += Model_ToolsChanged;
            model.ToolsCollectionChanged += Model_ToolsCollectionChanged;
            if (model.Tools != null)
                _items = model.Tools.ToList();
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(GetLayoutForModel(_items[position]), parent, false);
            if (_items[position] is MoodLevelModel)
                _context.LayoutInflater.Inflate(Resource.Layout.MoodLevelLayout, parent, false);
            if (view != null)
                HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, ToolModelBase toolModel)
        {
            //view.Click += (sender, e) => _model.CurrentTool = toolModel;
            Button detailsButton = view.FindViewById<Button>(Resource.Id.detailsButton);
            detailsButton.Click += (sender, e) => _model.CurrentTool = toolModel;
            MoodLevelModel moodLevelModel = toolModel as MoodLevelModel;
            if (moodLevelModel != null)
                HookMoodLevelModelToView(view, moodLevelModel);
            EmotionsPackModel emotionsPackModel = toolModel as EmotionsPackModel;
            if (emotionsPackModel != null)
                HookEmotionsPackModelToView(view, emotionsPackModel);
            MoneyCalculationModel moneyCalculationModel = toolModel as MoneyCalculationModel;
            if (moneyCalculationModel != null)
                HookMoneyCalculationModelToView(view, moneyCalculationModel);
        }

        private void HookMoodLevelModelToView(View view, MoodLevelModel moodLevelModel)
        {
            Button saveButton = view.FindViewById<Button>(Resource.Id.saveMoodChartButton);
            saveButton.Click += (sender, e) => moodLevelModel.Save();
            Button removeButton = view.FindViewById<Button>(Resource.Id.removeMoodChartButton);
            removeButton.Click += (sender, e) => moodLevelModel.Remove();
        }

        private void HookEmotionsPackModelToView(View view, EmotionsPackModel emotionsPackModel)
        {
            //EditText emotionsPackNameEditText = view.FindViewById<EditText>(Resource.Id.emotionsPackNameEditText);
            //emotionsPackNameEditText.Text = emotionsPackModel.NewTraceItemName;
            //emotionsPackNameEditText.TextChanged += (sender, e) => emotionsPackModel.NewTraceItemName = emotionsPackNameEditText.Text;
        }

        private void HookMoneyCalculationModelToView(View view, MoneyCalculationModel moneyCalculationModel)
        {
            DatePicker startDatePicker = view.FindViewById<DatePicker>(Resource.Id.startDatePicker);
            startDatePicker.UpdateDate(moneyCalculationModel.StartDate.Year, moneyCalculationModel.StartDate.Month, moneyCalculationModel.StartDate.Day);
            startDatePicker.Click += (sender, e) => moneyCalculationModel.StartDate = new DateTime(startDatePicker.Year, startDatePicker.Month, startDatePicker.DayOfMonth);
            DatePicker finishDatePicker = view.FindViewById<DatePicker>(Resource.Id.finishDatePicker);
            finishDatePicker.UpdateDate(moneyCalculationModel.FinishDate.Year, moneyCalculationModel.FinishDate.Month, moneyCalculationModel.FinishDate.Day);
            finishDatePicker.Click += (sender, e) => moneyCalculationModel.FinishDate = new DateTime(finishDatePicker.Year, finishDatePicker.Month, finishDatePicker.DayOfMonth);
            Button saveButton = view.FindViewById<Button>(Resource.Id.saveButton);
            saveButton.Click += (sender, e) =>
            {
                moneyCalculationModel.StartDate = new DateTime(startDatePicker.Year, startDatePicker.Month, startDatePicker.DayOfMonth);
                moneyCalculationModel.FinishDate = new DateTime(finishDatePicker.Year, finishDatePicker.Month, finishDatePicker.DayOfMonth);
                moneyCalculationModel.Save();
            };
            Button removeButton = view.FindViewById<Button>(Resource.Id.removeButton);
            removeButton.Click += (sender, e) => moneyCalculationModel.Remove();
        }

        private int GetLayoutForModel(ToolModelBase toolModelBase)
        {
            if (toolModelBase is MoodLevelModel)
                return Resource.Layout.MoodLevelLayout;
            if (toolModelBase is EmotionsPackModel)
                return Resource.Layout.EmotionsPackLayout;
            if (toolModelBase is MoneyCalculationModel)
                return Resource.Layout.MoneyCalculationLayout;
            throw new NotImplementedException();
        }

        private void Model_ToolsChanged(object sender, EventArgs e)
        {
            _context.RunOnUiThread(() =>
            {
                _items = _model.Tools.ToList();
                NotifyDataSetChanged();
            });
        }

        private void Model_ToolsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _context.RunOnUiThread(() =>
            {
                ObservableCollectionHelper.ApplyChanges<ToolModelBase>(e, _items);
                NotifyDataSetChanged();
            });
        }
    }
}